import {
	RECORD_DEFAULT_ADDRESS,
	ADD_CART,
	REDUCE_CART,
	WAREHOUSE_SELECT_CART,
	GOODS_SELECT_CART,
	WAREHOUSE_DELETE_CART,
	GOODS_DELETE_CART,
	RECORD_CART_GOODS_CHECK,
	INIT_BUYCART,
	CLEAR_CART,
	RECORD_USERINFO,
	GET_USERINFO,
	OUT_LOGIN,
	RETSET_NAME,
} from './mutation-types.js'

import {setStore, getStore} from '../config/mUtils'

import {localapi, proapi} from 'src/config/env'

export default {
	// 记录当前用户默认地址
	[RECORD_DEFAULT_ADDRESS](state, defaultAddress) {
		state.defaultAddress = defaultAddress;
		//存入localStorage
		setStore('defaultAddress', state.defaultAddress);
	},
	// 加入购物车
	[ADD_CART](state, {
		userId, //登录用户id
		warehouseCode, //仓库代码
		mechanismName, //机构名称
		startSendNum, //起送数量
		goodsClassNum, //起订种类
		deliveryMinAmount, //起送金额
		specificationId, //规格ID 
		specificationName, //规格名称
		gooodsId, //商品ID
		goodsName, //商品名称
		purchaseLimit, //购买上限
		stock, //库存
		goodsImg, //商品图片
		priceRange, //价格区间
		promotionId, //活动ID 
		isFullRe, //是否支持满减  
		isSupVou, //是否支持提货券 
		goodsCheck //校验返回信息
	}) {
		let cart = state.cartList;
		let use = cart[userId] = (cart[userId] || {});
		let warehouse = use[warehouseCode] = (use[warehouseCode] || {
			"warehouseCode" : warehouseCode,
			"mechanismName" : mechanismName,
			"startSendNum" : startSendNum, 
			"goodsClassNum" : goodsClassNum,
			"deliveryMinAmount" : deliveryMinAmount,
			"warehouseSelected" : false
		});
		let goodsList = warehouse['goodsList'] = (warehouse['goodsList'] || {});
		if(goodsList[specificationId]){
			goodsList[specificationId]['buyNum']++;
		}else{
			let minBuyNum = 1;
			if(priceRange){
				let priceList = JSON.parse(priceRange);
				minBuyNum = priceList[0].num
			}
			goodsList[specificationId] = {
				"specificationId" : specificationId, //规格ID 
				"specificationName" : specificationName, //规格名称
				"gooodsId" : gooodsId, //商品ID
				"goodsName" : goodsName, //商品名称
				"buyNum" : minBuyNum, //购买数量
				"purchaseLimit" : purchaseLimit, //购买上限
				"stock" : stock, //库存
				"goodsImg" : goodsImg, //商品图片
				"priceRange" : priceRange, //价格区间
				"promotionId" : promotionId, //活动ID 
				"isFullRe" : isFullRe, //是否支持满减  
				"isSupVou" : isSupVou, //是否支持提货券 
				"goodsCheck" : goodsCheck, //校验返回信息
				"selected" : false
			}
			warehouse['warehouseSelected'] = false;
		}
		state.cartList = {...cart};
		//存入localStorage
		setStore('buyCart', state.cartList);
	},
	// 移出购物车
	[REDUCE_CART](state, {
		userId, //登录用户id
		warehouseCode, //仓库代码
		specificationId, //规格ID 
		priceRange, //价格区间
	}) {
		let cart = state.cartList;
		let use = (cart[userId] || {});
		let warehouse = (use[warehouseCode] || {});
		let goodsList = (warehouse['goodsList'] || {});
		let minBuyNum = 1;
		if(priceRange){
			let priceList = JSON.parse(priceRange);
			minBuyNum = priceList[0].num
		}
		if (goodsList && goodsList[specificationId]) {
			if (goodsList[specificationId]['buyNum'] > minBuyNum) {
				goodsList[specificationId]['buyNum']--;
			} else {
				//商品数量为0，则清空当前商品的信息
				delete goodsList[specificationId]
			}
			if(Object.keys(goodsList).length == 0){
				//delete warehouse['goodsList']
				delete use[warehouseCode]
			}
			if(Object.keys(use).length < 1){
				delete cart[userId]
			}
		}
		state.cartList = {...cart};
		//存入localStorage
		setStore('buyCart', state.cartList);
	},
	// 购物车仓库选择
	[WAREHOUSE_SELECT_CART](state, {
		userId, //登录用户id
		warehouseCode, //仓库代码
	}) {
		let cart = state.cartList;
		let userCart = cart[userId];
        if(userCart&&userCart[warehouseCode]){
            userCart[warehouseCode]['warehouseSelected'] = userCart[warehouseCode]['warehouseSelected'] ? false : true;
            if(userCart[warehouseCode]['goodsList']){
                for(let key in userCart[warehouseCode]['goodsList']){
                    let goods = userCart[warehouseCode]['goodsList'][key];
                    if(goods.goodsCheck){
                		goods.selected = false;
                    }else{
                    	goods.selected = userCart[warehouseCode]['warehouseSelected'];
                    }
                }
            }
        }
		state.cartList = {...cart};
		//存入localStorage
		setStore('buyCart', state.cartList);
	},
	// 购物车商品选择
	[GOODS_SELECT_CART](state, {
		userId, //登录用户id
		warehouseCode, //仓库代码
		specificationId, //规格ID 
	}) {
		let cart = state.cartList;
		let userCart = cart[userId];
        if(userCart&&userCart[warehouseCode]&&userCart[warehouseCode]['goodsList']&&userCart[warehouseCode]['goodsList'][specificationId]){
            let goods = userCart[warehouseCode]['goodsList'][specificationId];
            if(goods.goodsCheck){
            	goods.selected = false;
            }else{
            	goods.selected = goods.selected ? false : true;
            }
            if(!goods.selected && !goods.goodsCheck){
                userCart[warehouseCode]['warehouseSelected'] = false;
            }else{
                let flag = true;
                for(let key in userCart[warehouseCode]['goodsList']){
                    let item = userCart[warehouseCode]['goodsList'][key];
                    if(!item.goodsCheck){
                    	flag = item.selected && flag;
                    }
                    if(!flag){
                        break;
                    }
                }
                userCart[warehouseCode]['warehouseSelected'] = flag;
            }
        }
		state.cartList = {...cart};
		//存入localStorage
		setStore('buyCart', state.cartList);
	},
	// 购物车删除整个仓库的商品
	[WAREHOUSE_DELETE_CART](state, {
		userId, //登录用户id
		warehouseCode, //仓库代码
	}) {
		let cart = state.cartList;
		let use = (cart[userId] || {});
		delete use[warehouseCode];
		if(Object.keys(use).length < 1){
			delete cart[userId]
		}
		state.cartList = {...cart};
		//存入localStorage
		setStore('buyCart', state.cartList);
	},
	// 购物车删除某个特定商品
	[GOODS_DELETE_CART](state, {
		userId, //登录用户id
		warehouseCode, //仓库代码
		specificationId, //规格ID
	}) {
		let cart = state.cartList;
		let use = cart[userId];
		let warehouse = use[warehouseCode];
		delete warehouse['goodsList'][specificationId];
		if(Object.keys(warehouse['goodsList']).length < 1){
			delete use[warehouseCode]
		}
		if(Object.keys(use).length < 1){
			delete cart[userId]
		}
		state.cartList = {...cart};
		//存入localStorage
		setStore('buyCart', state.cartList);
	},
	//更改购物车商品校验信息
	[RECORD_CART_GOODS_CHECK](state,{
		userId,
		warehouseCode,
		specificationId,
		goodsCheck
	}) {
		let cart = state.cartList;
		let userCart = cart[userId];
        if(userCart&&userCart[warehouseCode]&&userCart[warehouseCode]['goodsList']&&userCart[warehouseCode]['goodsList'][specificationId]){
            let goods = userCart[warehouseCode]['goodsList'][specificationId]
            goods.goodsCheck = goodsCheck;
            if(goodsCheck.length > 0){
            	goods.selected = false;
            }
        }
		state.cartList = {...cart};
		//存入localStorage
		setStore('buyCart', state.cartList);
	},	
	//网页初始化时从本地缓存获取购物车数据
	[INIT_BUYCART](state) {
		let initCart = getStore('buyCart');
		if (initCart) {
			state.cartList = JSON.parse(initCart);
		}
	},
	//清空当前商品的购物车信息
	[CLEAR_CART](state, shopid) {
		state.cartList[shopid] = null;
		state.cartList = {...state.cartList};
		setStore('buyCart', state.cartList);
	},
	// 记录用户信息
	[RECORD_USERINFO](state, info) {
		state.userInfo = info;
		state.login = true;
		setStore('user_id', info.id);
	},
	//获取用户信息存入vuex
	[GET_USERINFO](state, info) {
		if (state.userInfo && (state.userInfo.username !== info.username)) {
			return;
		};
		if (!state.login) {
			return
		}
		if (!info.message) {
			state.userInfo = {...info};
		} else {
			state.userInfo = null;
		}
	},
	//修改用户名
	[RETSET_NAME](state,username) {
		state.userInfo = Object.assign({}, state.userInfo,{username})
	},
	//退出登录
	[OUT_LOGIN](state) {
		state.userInfo = null;
		state.login = false;
	},
}
